package com.thoughtworks;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import static java.lang.System.exit;

public class LibraryManager {

    private Library library;
    private final HashMap<String, String> login;
    private boolean isLoggedIn;

    public LibraryManager() {
        ArrayList<Rental> rentable = new ArrayList<Rental>();
        rentable.add(new Books("Java Puzzlers", "Joshua Bloch", 2005));
        rentable.add(new Books("Python:The Complete Reference", "Martin C. Brown", 2001));
        rentable.add(new Books("Two States", "Chetan Bhagat", 2014));
        rentable.add(new Books("Success Series", "Sachchida Nand Jha", 2013));
        rentable.add(new Books("Frankenstein", "Mary Shelley", 1998));
        rentable.add(new Movies("Gilli", "Dharani", 2004, 9));
        rentable.add(new Movies("Veeram", "Siva", 2014, 8));
        rentable.add(new Movies("Karate Kid", "Harald Zwart", 2010, 9));

        login = new HashMap<String, String>();
        login.put("123-0001", "abc");
        login.put("123-0002", "def");
        login.put("123-0003", "ghi");
        this.isLoggedIn = false;

        this.library = new Library(rentable);
    }

    public static void main(String args[]) throws IOException {
        new LibraryManager().run();
    }

    public boolean userLoggedIn() {
        return this.isLoggedIn;
    }

    public void run() throws IOException {
        welcomeMessage();
        String libraryNumber = "";
        String password;

        Scanner keyboard = new Scanner(System.in);
        while (true) {
            if (isLoggedIn) {
                System.out.println();
                menu();
                int mainMenuChoice = getChoice();
                menuAction(login, libraryNumber, mainMenuChoice);

            } else {
                System.out.println("Enter the Library Number");
                libraryNumber = keyboard.nextLine();
                System.out.println("Enter the password");
                password = keyboard.nextLine();
                userLogin(login, libraryNumber, password);
                if (!this.isLoggedIn)
                    System.out.println("Invalid user");
            }
        }
    }

    public void userLogin(HashMap<String, String> login, String libraryNumber, String password) {
        String value = "";

        for (String key : login.keySet())
            if (libraryNumber.equals(key)) {
                value = login.get(key);
                break;
            }

        isLoggedIn = value.equals(password);
    }

    public void welcomeMessage() {
        System.out.println("        WELCOME TO BIBLIOTECA!      ");
        System.out.println("------------------------------------");
    }

    public void menu() {
        System.out.println("Choose your action");
        System.out.println("    1. My Profile");
        System.out.println("    2. List Books & Movies");
        System.out.println("    3. LogOut");
        System.out.println("    4. Quit");
        System.out.println("Select a valid option");

    }

    public void checkoutOrReturn() {
        System.out.println();
        System.out.println("Enter your choice");
        System.out.println("    1.Checkout an item");
        System.out.println("    2.Return an item");
    }

    public int getChoice() {
        Scanner in = new Scanner(System.in);
        int yourChoice = in.nextInt();
        return yourChoice;
    }

    public void menuAction(HashMap<String, String> login, String libraryNumber, int choice) throws IOException {
        switch (choice) {
            case 1:
                String userDetails[] = (new UserDetails()).userInfo(libraryNumber);
                System.out.println("Name:" + userDetails[0] + "\nEmailId:" + userDetails[1] + "\nPhoneNumber:" + userDetails[2] + "\n");
                menu();
                int mainMenuChoice = getChoice();
                menuAction(login, libraryNumber, mainMenuChoice);
                break;
            case 2:
                library.displayCatalog(this);
                checkoutOrReturn();
                Scanner input = new Scanner(System.in);
                int subMenuChoice = input.nextInt();
                if(input.hasNextLine())
                    input.nextLine();
                if (subMenuChoice == 1 || subMenuChoice == 2) {
                    System.out.println("\nEnter the name of the item");
                }
                String name = input.nextLine();
                String message = subMenuAction(libraryNumber, subMenuChoice, name);
                System.out.println(message);
                break;
            case 3:
                System.out.println("Thank you! Visit again");
                this.isLoggedIn = false;
                break;
            case 4:
                library.displayCheckoutInformation();
                exit(0);
        }

    }


    public String subMenuAction(String libraryNumber, int choice, String name) {
        String result = null;
        switch (choice) {
            case 1:
                boolean checkoutBook = library.checkOutItem(libraryNumber, name);
                if (checkoutBook)
                    result = "Thank you! Enjoy the item";
                else
                    result = "That item is not available.";

                break;

            case 2:
                boolean returnbook = library.returnItem(libraryNumber, name);
                if (returnbook)
                    result = "Thank you for returning the item";
                else
                    result = "That is not a valid item to return.";
                break;


            default:
                System.out.println();
        }
        return result;
    }

    public void displayItem(Rental name) {
        name.getDetail();

    }

}

